/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.spring.transaction.bean;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.basics.exception.technical.WrapperException;

/**
 * Template class that simplifies programmatic transaction demarcation and
 * transaction exception handling.
 * <p>
 * The central method is execute(TransactionCallback), supporting transactional
 * code that implements the TransactionCallback interface. This template handles
 * the transaction lifecycle and possible exceptions such that neither the
 * TransactionCallback implementation nor the calling code needs to explicitly
 * handle transactions.
 * <p>
 * Typical usage: Allows for writing low-level data access objects that use
 * resources such as JDBC DataSources but are not transaction-aware themselves.
 * Instead, they can implicitly participate in transactions handled by
 * higher-level application services utilizing this class, making calls to the
 * low-level services via an inner-class callback object.
 * <p>
 * Can be used within a service implementation via direct instantiation with a
 * transaction manager reference, or get prepared in an application context and
 * passed to services as bean reference. Note: The transaction manager should
 * always be configured as bean in the application context: in the first case
 * given to the service directly, in the second case given to the prepared
 * template.
 * <p>
 * Inspired deeply from <code>TransactionTemplate</code> of Spring Framework.
 * 
 * @see <a
 *      href="http://www.springframework.org/docs/api/org/springframework/transaction/support/TransactionTemplate.html"><code>TransactionTemplate</code>
 *      of Spring Framework</a>
 */
public class TransactionTemplate
{

  /**
   * the {@link TransactionTemplate} to delegate to.
   */
  private transient final org.springframework.transaction.support.TransactionTemplate delegate;



  /**
   * Construct a new TransactionTemplate using the given transaction manager.
   * 
   * @param transactionManager
   *          the transaction management strategy to be used
   */
  public TransactionTemplate(PlatformTransactionManager transactionManager)
  {
    this.delegate = new org.springframework.transaction.support.TransactionTemplate(transactionManager);
  }



  /**
   * Execute the action specified by the given callback object within a
   * transaction.
   * <p>
   * Allows for returning a result object created within the transaction, that
   * is, a domain object or a collection of domain objects. A RuntimeException
   * thrown by the callback is treated as a fatal exception that enforces a
   * rollback. Such an exception gets propagated to the caller of the template.
   * 
   * @param action
   *          the callback object that specifies the transactional action
   * @return a result object returned by the callback, or <code>null</code> if
   *         none
   * @throws BundledException
   *           if the underlying action throwed one
   */
  public Object execute(final TransactionCallback action)
      throws BundledException
  {
    try {
      return delegate.execute(new org.springframework.transaction.support.TransactionCallback()
      {

        public Object doInTransaction(TransactionStatus status)
        {
          try {
            return action.doInTransaction(status);
          }
          catch (BundledException e) {
            throw new WrapperException(e);
          }
        }

      });
    }
    catch (WrapperException e) {
      throw (BundledException) e.getCause();
    }

  }

}
